package com.xbfinal.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbfinal.commonUtils.Result;
import com.xbfinal.eduservice.entity.EduCourse;
import com.xbfinal.eduservice.entity.EduTeacher;
import com.xbfinal.eduservice.entity.vo.CourseInfoVo;
import com.xbfinal.eduservice.entity.vo.CoursePublishVo;
import com.xbfinal.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xbfinal
 * @since 2023-01-09
 */
@Slf4j
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;


    /**
     * 添加课程的基本信息
     * @param courseInfoVo
     * @return
     */
    @ApiOperation("添加课程的基本信息")
    @PostMapping("/addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        String  id =  eduCourseService.saveCourseInfo(courseInfoVo);
        if(id!=null){
            return Result.ok().add("courseId",id);
        }
        return Result.failed();
    }


    /**
     * 根据id查询信息
     * @param courseId
     * @return
     */
    @ApiOperation("根据id查询信息")
    @GetMapping("/getCourseById/{courseId}")
    public Result getCourseById(@PathVariable("courseId") String courseId){
        log.info("/getCourseById/+courseId");
        CourseInfoVo courseInfoVo =eduCourseService.getCourseInfoVo(courseId);

        return Result.ok().add("courseInfoVo",courseInfoVo);
    }

    @ApiOperation("修改课程信息")
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        eduCourseService.updaCourseInfo(courseInfoVo);

        return Result.ok();
    }

    @ApiOperation("根据课程id查询确认信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public Result getPublishCourseInfo(@PathVariable("id") String id){
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(id);
        log.info(coursePublishVo.getCover());
        return Result.ok().add("PublishCourse",coursePublishVo);
    }

    @ApiOperation("课程最终发布")
    @PostMapping("publishCourse/{id}")
    public Result publishCourse(@PathVariable("id") String id){
        log.info("课程最终发布");
//        这里直接创建对象然后根据id更新就行 不要查数据库
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return  Result.ok();
    }

//    @ApiOperation("课程列表")
//    @GetMapping
//    public Result getListCourse(){
//        List<EduCourse> list = eduCourseService.list(null);
//        log.info("课程列表",list);
//        return  Result.ok().addData(list);
//    }

    /**
     * TODO 带条件的分页查询
     * @param current
     * @param limit
     * @return
     */
    @ApiOperation("课程列表分页查询")
    @GetMapping("{current}/{limit}")
    public Result getListCourse(@ApiParam(name = "current",value = "当前页码")
                                    @PathVariable("current") Long current,
                                @ApiParam(name = "limit",value = "条数")
                                    @PathVariable("limit") Long limit){


        log.info("课程列表分页查询"+current+"条数"+limit);
        //创建page对象 参数1页码 参数2条数
        Page<EduCourse> page=new Page<>(current,limit);
        //调用Service.page 方法实现分页 参数1 page对象 参数2 条件
        IPage<EduCourse> page1 = eduCourseService.page(page, null);
        return  Result.ok().addData(page1);
    }



    @ApiOperation("删除课程")
    @DeleteMapping("{courseId}")
    public Result deleteCourse(@PathVariable("courseId") String courseId){
        eduCourseService.removeCourse(courseId);
        return Result.ok();
    }


}

